package com.jshcbd.modules.asset.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.jshcbd.common.util.DateUtils;
import com.jshcbd.framework.poi.excel.annotation.Excel;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;

/**
 * @author zhoucd
 * @date
 * @description
 */
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel( description = "资产折旧")
public class DepreciationVo extends AssetVo implements Serializable {


    public DepreciationVo(){
    }

    public DepreciationVo(AssetVo assetVo){
        BeanUtils.copyProperties(assetVo,this);
    }

    /**
     * 获取当前折旧
     * @param month
     * @return
     */
    private BigDecimal getCurrent(Integer month) {
        Integer diffMonth = this.getDiffMonth(month);

        //入库时间之前、处置的或者入库时间超过5年的资产月折旧额为0
        if(diffMonth <=0 || this.isDis(month) || diffMonth > this.getSumMonth()
        ){
            return BigDecimal.ZERO;
        }
        return this.avg();
    }

    private Integer getSumMonth(){
        return 5*12;
    }


    private BigDecimal avg(){
        return this.getPrice().multiply(new BigDecimal(0.95))
                .divide(new BigDecimal(this.getSumMonth()) ,2,RoundingMode.HALF_UP);
    }

    /**
     * 获取累计折旧
     * @param month
     * @return
     */
    private BigDecimal getSum(Integer month) {
        Integer diffMonth = this.getDiffMonth(month);
        //入库时间之前或者处置的资产月折旧额为0
        if(diffMonth<=0){
            return BigDecimal.ZERO;
        }
        if(isDis(month)){
            diffMonth = this.getDiffMonth(this.getDisDate(),this.getInDate());
        }

        if( diffMonth>(5*12)){
            return this.getPrice().multiply(new BigDecimal(0.95))
                    .setScale(2,RoundingMode.HALF_UP);
        }
        return this.avg().multiply(new BigDecimal(diffMonth));
    }


    /**
     * 残值
     * @param month
     * @return
     */
    private BigDecimal getSurplus(Integer month) {
        Integer diffMonth = this.getDiffMonth(month);
        if(diffMonth<=0 || this.isDis(month)){
            return BigDecimal.ZERO;
        }
        if( diffMonth >this.getSumMonth()){
            return this.getPrice().multiply(new BigDecimal(0.05))
                    .setScale(2,RoundingMode.HALF_UP);
        }
        return this.getPrice().subtract(this.getSum(month));
    }


    /**
     * 当前 year month 和入库日期差
     * @param month
     * @return
     */
    private Integer getDiffMonth(Integer month){
        Calendar calendar1 = new Calendar.Builder().build();
        calendar1.setTime(this.getInDate());
        calendar1.set(Calendar.DATE,1);
        Calendar calendar2 = new Calendar.Builder().build();
        calendar2.set(this.year,month,1);
        Integer diffMonth =  DateUtils.dateDiff('M',calendar2,calendar1);
        return diffMonth;
    }

    /**
     * 当前 year month 和入库日期差
     * @param month
     * @return
     */
    private Integer getDiffMonth(Date date1, Date date2){
        Calendar calendar1 = new Calendar.Builder().build();
        calendar1.setTime(date2);
        calendar1.set(Calendar.DATE,1);
        Calendar calendar2 = new Calendar.Builder().build();
        calendar2.setTime(date1);
        calendar2.set(Calendar.DATE,1);
        Integer diffMonth =  DateUtils.dateDiff('M',calendar2,calendar1);
        return diffMonth;
    }


    /**
     * 当前year month 资产是否被处置
     * @param month
     * @return
     */
    public boolean isDis(Integer month){
        if(this.getState() == 3){
            Calendar calendar1 = new Calendar.Builder().build();
            calendar1.setTime( this.getDisDate());
            calendar1.set(Calendar.DATE,1);
            Calendar calendar2 = new Calendar.Builder().build();
            calendar2.set(this.year,month,1);
            Integer diffMonth =  DateUtils.dateDiff('M',calendar1,calendar2);
            if(diffMonth<0){
                return true;
            }
        }
        return  false;
    }



    public BigDecimal getCurrent_1() {
        return this.getCurrent(1);
    }

    public BigDecimal getSum_1() {
        return this.getSum(1);
    }

    public BigDecimal getSurplus_1() {
        return this.getSurplus(1);
    }

    public BigDecimal getCurrent_2() {
        return this.getCurrent(2);
    }

    public BigDecimal getSum_2() {
        return this.getSum(2);
    }

    public BigDecimal getSurplus_2() {
        return this.getSurplus(2);
    }

    public BigDecimal getCurrent_3() {
        return this.getCurrent(3);
    }

    public BigDecimal getSum_3() {
        return this.getSum(3);
    }

    public BigDecimal getSurplus_3() {
        return this.getSurplus(3);
    }

    public BigDecimal getCurrent_4() {
        return this.getCurrent(4);
    }

    public BigDecimal getSum_4() {
        return this.getSum(4);
    }

    public BigDecimal getSurplus_4() {
        return this.getSurplus(4);
    }

    public BigDecimal getCurrent_5() {
        return this.getCurrent(5);
    }

    public BigDecimal getSum_5() {
        return this.getSum(5);
    }

    public BigDecimal getSurplus_5() {
        return this.getSurplus(5);
    }

    public BigDecimal getCurrent_6() {
        return this.getCurrent(6);
    }

    public BigDecimal getSum_6() {
        return this.getSum(6);
    }

    public BigDecimal getSurplus_6() {
        return this.getSurplus(6);
    }

    public BigDecimal getCurrent_7() {
        return this.getCurrent(7);
    }

    public BigDecimal getSum_7() {
        return this.getSum(7);
    }

    public BigDecimal getSurplus_7() {
        return this.getSurplus(7);
    }

    public BigDecimal getCurrent_8() {
        return this.getCurrent(8);
    }

    public BigDecimal getSum_8() {
        return this.getSum(8);
    }

    public BigDecimal getSurplus_8() {
        return this.getSurplus(8);
    }

    public BigDecimal getCurrent_9() {
        return this.getCurrent(9);
    }

    public BigDecimal getSum_9() {
        return this.getSum(9);
    }

    public BigDecimal getSurplus_9() {
        return this.getSurplus(9);
    }

    public BigDecimal getCurrent_10() {
        return this.getCurrent(10);
    }

    public BigDecimal getSum_10() {
        return this.getSum(10);
    }

    public BigDecimal getSurplus_10() {
        return this.getSurplus(10);
    }

    public BigDecimal getCurrent_11() {
        return this.getCurrent(11);
    }

    public BigDecimal getSum_11() {
        return this.getSum(11);
    }

    public BigDecimal getSurplus_11() {
        return this.getSurplus(11);
    }

    public BigDecimal getCurrent_12() {
        return this.getCurrent(12);
    }

    public BigDecimal getSum_12() {
        return this.getSum(12);
    }

    public BigDecimal getSurplus_12() {
        return this.getSurplus(12);
    }


    @Excel(groupName = "",name = "年份")
    private Integer year;



    public Integer getYear(){
        return this.year;
    }

    public DepreciationVo setYear(Integer year){
        this.year = year;
        return this;
    }


    @ApiModelProperty(value = "编号ID",required=true)
    @NotNull
    @Excel(name="资产ID")
    private String number;


    @ApiModelProperty(value = "资产名称",required=true)
    @NotNull
    @Excel(name="资产名称")
    private String name;


    @ApiModelProperty(value = "合同编号",required=true)
    @NotNull
    @Excel(name="合同编号")
    private String contractNamber;


    @ApiModelProperty(value = "批次",required=true)
    @NotNull
    @Excel(name="批次")
    private String batch;


    @ApiModelProperty(value = "入库日期",required=true)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @NotNull
    @Excel(name="入库日期",format="yyyy-MM-dd")
    private Date inDate;


    @ApiModelProperty(value = "价格",required=true)
    @NotNull
    @Excel(name = "价格")
    private BigDecimal price;


    @ApiModelProperty(value = "当期折旧")
    @Excel(groupName = "1月份", name = "当期折旧")
    private BigDecimal current_1;


    @ApiModelProperty(value = "累计折旧")
    @Excel(groupName = "1月份",name =  "累计折旧")
    private BigDecimal sum_1;

    @ApiModelProperty(value = "剩余值")
    @Excel(groupName = "1月份",name =  "剩余值")
    private BigDecimal surplus_1;


    @ApiModelProperty(value = "当期折旧")
    @Excel(groupName = "2月份",name =  "当期折旧")
    private BigDecimal current_2;

    @ApiModelProperty(value = "累计折旧")
    @Excel(groupName = "2月份",name =  "累计折旧")
    private BigDecimal sum_2;

    @ApiModelProperty(value = "剩余值")
    @Excel(groupName = "2月份",name =  "剩余值")
    private BigDecimal surplus_2;

    @ApiModelProperty(value = "当期折旧")
    @Excel(groupName = "3月份",name =  "当期折旧")
    private BigDecimal current_3;

    @ApiModelProperty(value = "累计折旧")
    @Excel(groupName = "3月份",name =  "累计折旧")
    private BigDecimal sum_3;

    @ApiModelProperty(value = "剩余值")
    @Excel(groupName = "3月份",name =  "剩余值")
    private BigDecimal surplus_3;

    @ApiModelProperty(value = "当期折旧")
    @Excel(groupName = "4月份",name =  "当期折旧")
    private BigDecimal current_4;

    @ApiModelProperty(value = "累计折旧")
    @Excel(groupName = "4月份",name =  "累计折旧")
    private BigDecimal sum_4;

    @ApiModelProperty(value = "剩余值")
    @Excel(groupName = "4月份",name =  "剩余值")
    private BigDecimal surplus_4;

    @ApiModelProperty(value = "当期折旧")
    @Excel(groupName = "5月份",name =  "当期折旧")
    private BigDecimal current_5;

    @ApiModelProperty(value = "累计折旧")
    @Excel(groupName = "5月份",name =  "累计折旧")
    private BigDecimal sum_5;

    @ApiModelProperty(value = "剩余值")
    @Excel(groupName = "5月份",name =  "剩余值")
    private BigDecimal surplus_5;

    @ApiModelProperty(value = "当期折旧")
    @Excel(groupName = "6月份",name =  "当期折旧")
    private BigDecimal current_6;

    @ApiModelProperty(value = "累计折旧")
    @Excel(groupName = "6月份",name =  "累计折旧")
    private BigDecimal sum_6;

    @ApiModelProperty(value = "剩余值")
    @Excel(groupName = "6月份",name =  "剩余值")
    private BigDecimal surplus_6;

    @ApiModelProperty(value = "当期折旧")
    @Excel(groupName = "7月份",name =  "当期折旧")
    private BigDecimal current_7;

    @ApiModelProperty(value = "累计折旧")
    @Excel(groupName = "7月份",name =  "累计折旧")
    private BigDecimal sum_7;

    @ApiModelProperty(value = "剩余值")
    @Excel(groupName = "7月份",name =  "剩余值")
    private BigDecimal surplus_7;

    @ApiModelProperty(value = "当期折旧")
    @Excel(groupName = "8月份",name =  "当期折旧")
    private BigDecimal current_8;

    @ApiModelProperty(value = "累计折旧")
    @Excel(groupName = "8月份",name =  "累计折旧")
    private BigDecimal sum_8;

    @ApiModelProperty(value = "剩余值")
    @Excel(groupName = "8月份",name =  "剩余值")
    private BigDecimal surplus_8;

    @ApiModelProperty(value = "当期折旧")
    @Excel(groupName = "9月份",name =  "当期折旧")
    private BigDecimal current_9;

    @ApiModelProperty(value = "累计折旧")
    @Excel(groupName = "9月份",name =  "累计折旧")
    private BigDecimal sum_9;

    @ApiModelProperty(value = "剩余值")
    @Excel(groupName = "9月份",name =  "剩余值")
    private BigDecimal surplus_9;

    @ApiModelProperty(value = "当期折旧")
    @Excel(groupName = "10月份",name =  "当期折旧")
    private BigDecimal current_10;

    @ApiModelProperty(value = "累计折旧")
    @Excel(groupName = "10月份",name =  "累计折旧")
    private BigDecimal sum_10;

    @ApiModelProperty(value = "剩余值")
    @Excel(groupName = "10月份",name =  "剩余值")
    private BigDecimal surplus_10;

    @ApiModelProperty(value = "11月份_当期折旧")
    @Excel(groupName = "11月份",name =  "当期折旧")
    private BigDecimal current_11;

    @ApiModelProperty(value = "11月份_累计折旧")
    @Excel(groupName = "11月份",name =  "累计折旧")
    private BigDecimal sum_11;

    @ApiModelProperty(value = "11月份_剩余值")
    @Excel(groupName = "11月份",name =  "剩余值")
    private BigDecimal surplus_11;

    @ApiModelProperty(value = "12月份_当期折旧")
    @Excel(groupName = "12月份",name =  "当期折旧")
    private BigDecimal current_12;

    @ApiModelProperty(value = "12月份_累计折旧")
    @Excel(groupName = "12月份",name =  "累计折旧")
    private BigDecimal sum_12;

    @ApiModelProperty(value = "12月份_剩余值")
    @Excel(groupName = "12月份",name =  "剩余值")
    private BigDecimal surplus_12;


}
